<?php
namespace app\admin\controller;
use think\Db;
use app\admin\model\Users as UsersModel;
class Deal extends Common{

	public function index(){
		if(request()->isPost()){
            $key=input('post.key');
            $page =input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):config('pageSize');
            $list=db('bs_order')->alias('a')
            	->join('users b','a.uid = b.id')
            	->where('b.nickname','like',"%".$key."%")
            	->where('types',0)
            	->order('a.addtime desc')
            	->field('a.*,b.nickname')
            	->paginate(array('list_rows'=>$pageSize,'page'=>$page))
            	->toArray();
            /*db('guamai')
			    ->alias('u')
                ->join('__USERS__ ul','ul.id = u.uid','left')
                ->field('u.id as bid,u.num,u.price,u.is_ok,u.gid,u.gnickname,u.createtime as utime,u.unickname,u.uid as uuid,ul.*')
                ->where('ul.nickname|ul.mobile|ul.username','like',"%".$key."%")
                ->order('u.id desc')
                ->paginate(array('list_rows'=>$pageSize,'page'=>$page))
                ->toArray();*/
             if(!empty($list)){
                 foreach ($list['data'] as $k=>$v){
                     $list['data'][$k]['addtime'] = date('Y-m-d H:s',$v['addtime']);
                 }
             }
            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }
		return $this->fetch();

	}
	public function trading(){

		if(request()->isPost()){
            $key=input('post.key');
            $page =input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):config('pageSize');
            $list=db('bs_order')->alias('a')
            	->join('users b','a.uid = b.id')
            	->where('b.nickname','like',"%".$key."%")
            	->where('types',2)
            	->order('a.addtime desc')
            	->field('a.*,b.nickname')
            	->paginate(array('list_rows'=>$pageSize,'page'=>$page))
            	->toArray();
            /*db('guamai')
			    ->alias('u')
                ->join('__USERS__ ul','ul.id = u.uid','left')
                ->field('u.id as bid,u.num,u.price,u.is_ok,u.gid,u.gnickname,u.createtime as utime,u.unickname,u.uid as uuid,ul.*')
                ->where('ul.nickname|ul.mobile|ul.username','like',"%".$key."%")
                ->order('u.id desc')
                ->paginate(array('list_rows'=>$pageSize,'page'=>$page))
                ->toArray();*/
            if(!empty($list)){
                foreach ($list['data'] as $k=>$v){
                 	$relative = Db::name('users')->where(array('id'=>$v['relative_id']))->field('nickname')->find();
                    $list['data'][$k]['addtime'] = date('Y-m-d H:s',$v['addtime']);
                    $list['data'][$k]['amendtime'] = $v['amendtime']?date('Y-m-d H:s',$v['amendtime']):"";
                    $list['data'][$k]['endtime'] = $v['endtime']?date('Y-m-d H:s',$v['endtime']):"";
                    $list['data'][$k]['relativename'] = $relative['nickname'];
                    $list['data'][$k]['name'] = $v['remit_img']?'付款截图':'未付款';
                }
            }
            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }
		return $this->fetch();

	}
	//强制完成
	public function coerce_order(){

		$id = input('id');

		$order = Db::name('bs_order')->where(array('id'=>$id,'types'=>2))->find();

		if(!$order){

			return ['code'=>0,'msg'=>'订单不存在，或已经完成'];

		}
		$buyId = '';
		$sellId = '';
		if($order['b_or_s'] == 1 ){

			$buyId = $order['uid'];

			$sellId = $order['relative_id'];

		}
		if($order['b_or_s'] == 2){

			$buyId = $order['relative_id'];

			$sellId = $order['uid'];

		}
		if(!$buyId || !$sellId){

			return self::return_array(0001,'订单异常！',[]);

		}
		Db::name('users')->where(array('id'=>$sellId))->setDec('money_jifen_freeze',$order['money']);
		Db::name('users')->where(array('id'=>$buyId))->setInc('money_jifen',$order['money']);
		if(!Db::name('bs_order')->where(array('id'=>$order['id']))->update(array('types'=>1,'endtime'=>time()))){

			return ['code'=>0,'msg'=>'服务器异常'];

		}
		$beizhu = "买入到储蓄账户，买入金额:".$order['money'];

		mey_log(1,$buyId,1,20,$order['money'],$this->get_jifen($buyId),$beizhu);

		return ['code'=>1,'msg'=>'交易完成'];
	}
	//获取当前储蓄钱包
	public function get_jifen($id){

		$balance = Db::name('users')->where(array('id'=>$id))->field('money_benjin')->find();

		return $balance['money_jifen'];

	}
	public function accomplish (){

		if(request()->isPost()){
            $key=input('post.key');
            $page =input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):config('pageSize');
            $list=db('bs_order')->alias('a')
            	->join('users b','a.uid = b.id')
            	->where('b.nickname','like',"%".$key."%")
            	->where('types',1)
            	->order('a.addtime desc')
            	->field('a.*,b.nickname')
            	->paginate(array('list_rows'=>$pageSize,'page'=>$page))
            	->toArray();
            /*db('guamai')
			    ->alias('u')
                ->join('__USERS__ ul','ul.id = u.uid','left')
                ->field('u.id as bid,u.num,u.price,u.is_ok,u.gid,u.gnickname,u.createtime as utime,u.unickname,u.uid as uuid,ul.*')
                ->where('ul.nickname|ul.mobile|ul.username','like',"%".$key."%")
                ->order('u.id desc')
                ->paginate(array('list_rows'=>$pageSize,'page'=>$page))
                ->toArray();*/
            if(!empty($list)){
                foreach ($list['data'] as $k=>$v){
                 	$relative = Db::name('users')->where(array('id'=>$v['relative_id']))->field('nickname')->find();
                    $list['data'][$k]['addtime'] = date('Y-m-d H:s',$v['addtime']);
                    $list['data'][$k]['amendtime'] = $v['amendtime']?date('Y-m-d H:s',$v['amendtime']):"";
                    $list['data'][$k]['endtime'] = $v['endtime']?date('Y-m-d H:s',$v['endtime']):"";
                    $list['data'][$k]['relativename'] = $relative['nickname'];
                    $list['data'][$k]['name'] = $v['remit_img']?'付款截图':'未付款';
                }
            }
            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }
		return $this->fetch();

	}

 }